package com.java.zhuiyun.api.recommendedCourse.service.impl;

import com.java.zhuiyun.api.back_course.entity.BackCourse;
import com.java.zhuiyun.api.book_mark.entity.BookMark;
import com.java.zhuiyun.api.book_mark.mapper.BookMarkMapper;
import com.java.zhuiyun.api.book_mark.service.BookMarkService;
import com.java.zhuiyun.api.recommendedCourse.mapper.RecommendedCourseMapper;
import com.java.zhuiyun.api.recommendedCourse.model.TeacherCourseModel;
import com.java.zhuiyun.api.recommendedCourse.service.RecommendedCourseService;
import com.java.zhuiyun.api.series_course.model.PcSeriesCourse;
import com.java.zhuiyun.api.teacher.model.TeacherModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName RecommendedCourseServiceImpl
 * @Description TODO PC推荐课程接口实现
 * @Author WX
 * @Date 2023/8/21 9:23
 **/
@Service
public class RecommendedCourseServiceImpl implements RecommendedCourseService {
    @Autowired
    private RecommendedCourseMapper recommendedCourseMapper;

    @Autowired
    private BookMarkMapper bookMarkMapper;

    /*
     * @Author WX
     * @Description 推荐课程随机查询三条
     * @Date 2023/8/21 9:34
     * @Return
     * @param null
     */
    @Override
    public List<TeacherCourseModel> randonThree() {
        return recommendedCourseMapper.randonThree();
    }

    /*
     * @Author WX
     * @Description PC免费课程查询四条
     * @Date 2023/8/21 14:11
     * @Return
     * @param null
     */
    @Override
    public List<TeacherCourseModel> randonFour() {
        return recommendedCourseMapper.randonFour();
    }

    /*
     * @Author WX
     * @Description PC新课推荐
     * @Date 2023/8/21 15:33
     * @Return
     * @param null
     */
    @Override
    public List<TeacherCourseModel> randonNewFour() {
        return recommendedCourseMapper.randonNewFour();
    }

    /*
     * @Author WX
     * @Description PC精选好课
     * @Date 2023/8/21 16:06
     * @Return
     * @param null
     */
    @Override
    public List<TeacherCourseModel> randonGoodFour() {
        return recommendedCourseMapper.randonGoodFour();
    }

    /*
     * @Author WX
     * @Description PC系列课程
     * @Date 2023/8/21 16:25
     * @Return
     * @param null
     */
    @Override
    public List<PcSeriesCourse> randonSeriesFour() {
        return recommendedCourseMapper.randonSeriesFour();
    }

    /*
     * @Author WX
     * @Description PC精品课
     * @Date 2023/8/21 16:07
     * @Return
     * @param null
     */
    @Override
    public List<TeacherCourseModel> randonClsaaicFour() {
        return recommendedCourseMapper.randonClsaaicFour();
    }

    /*
     * @Author WX
     * @Description PC直播
     * @Date 2023/8/21 16:07
     * @Return
     * @param null
     */
    @Override
    public List<TeacherCourseModel> publicClasses() {
        return recommendedCourseMapper.publicClasses();
    }

//    /*
//     * @Author WX
//     * @Description 推荐课程随机查询三条 带参
//     * @Date 2023/8/21 9:34
//     * @Return
//     * @param subjectId
//     */
//    @Override
//    public List<TeacherCourseModel> randonThreeY(String subjectId) {
//        return recommendedCourseMapper.randonThreeY(subjectId);
//    }

    /*
     * @Author WX
     * @Description PC微专业
     * @Date 2023/8/22 11:15
     * @Return
     * @param null
     */
    @Override
    public List<TeacherCourseModel> selectMicroMajor() {
        return recommendedCourseMapper.selectMicroMajor();
    }

    /*
     * @Author WX
     * @Description 首页系列课程查询
     * @Date 2023/8/31 10:25
     * @Return
     * @param pcSeriesCourse
     */
    @Override
    public List<PcSeriesCourse> getSeriesCourseById(PcSeriesCourse pcSeriesCourse) {
        String courseIds = pcSeriesCourse.getCourseIds();
        String[] courseIdArray = courseIds.split(",");
        List<String> courseIdList = Arrays.asList(courseIdArray);
        List<PcSeriesCourse> list = recommendedCourseMapper.getSeriesCourseById(courseIdList);

        if (pcSeriesCourse.getUserId() == null) {
            for (PcSeriesCourse course : list) {
                course.setBookMarkId(0);
            }
            return list;
        } else {
            Integer userId = pcSeriesCourse.getUserId();
            List<BookMark> uList = bookMarkMapper.getSeriesCourseByUserId(userId, courseIdList);
            if (uList.isEmpty()) {
                for (PcSeriesCourse course : list) {
                    course.setBookMarkId(0);
                }
            } else {
                List<Integer> nonEmptyCourseIds = uList.stream()
                        .filter(bookMark -> bookMark.getCourseId() != null)
                        .map(BookMark::getCourseId)
                        .collect(Collectors.toList());

                for (PcSeriesCourse course : list) {
                    if (nonEmptyCourseIds.contains(course.getId())) {
                        course.setBookMarkId(1);
                    } else {
                        course.setBookMarkId(0);
                    }
                }
            }
            return list;
        }
    }

    /*
     * @Author WX
     * @Description 系列课程综查询总金额
     * @Date 2023/8/31 10:27
     * @Return
     * @param pcSeriesCourse
     */
    @Override
    public Double getSeriesCoursePrice(PcSeriesCourse pcSeriesCourse) {
        String courseIds = pcSeriesCourse.getCourseIds();
        String[] courseIdArray = courseIds.split(",");
        List<String> pricelist = Arrays.asList(courseIdArray);
        List<PcSeriesCourse> list = recommendedCourseMapper.getSeriesCoursePrice(pricelist);
        BigDecimal totalPrice = BigDecimal.ZERO;

        for (PcSeriesCourse course : list) {
            BigDecimal coursePrice = course.getCoursePrice();
            totalPrice = totalPrice.add(coursePrice);
        }
        return totalPrice.doubleValue();
    }
}
